package com.aiju.app.config;

import com.aiju.app.DataProviderManager;
import com.aiju.bean.User;
import com.aiju.utils.PreferencesUtils;

import java.io.File;

/**
 * @author: 佐为
 * @date: 2016-11-08 14:10
 * @version: V1.0
 * @Description: 项目配置管理类
 */
public class ConfigManager {

    //服务器路径,默认正式版路径
    public static String INIT_SERVER_HOST = "https://store.ecbao.cn";
    public static String INIT_PAY_HOST = "https://trade.ecbao.cn";

    //常量类
    public static final String DIR_SD_ROOT = android.os.Environment.getExternalStorageDirectory().getAbsolutePath();
    public static final String DIR_APP_ROOT = "/Aiiju";
    public static final String DIR_IMAGE = DIR_SD_ROOT + DIR_APP_ROOT + "/image/";
    public static final String DIR_CACHE = DIR_SD_ROOT + DIR_APP_ROOT + "/cache/";
    public static final String DIR_ERROR = DIR_SD_ROOT + DIR_APP_ROOT + "/QRCode/";
    public static final String DIR_LOG = DIR_SD_ROOT + DIR_APP_ROOT + "/log/";

    private static final ConfigManager configManager = new ConfigManager();

    //是否进入dubug模式，打印日志信息
    public static boolean DEBUG = true;

    //日志打印TAG
    public static final String DEBUG_TAG = "AiJu"; //系统

    public static final String DEBUG_LIFECYCLE_TAG = "Lifecycle"; //生命周期

    public static final String DEBUG_HTTP_TAG = "Http"; //网络请求

    //极光注册id
    private String regId = "a";

    /**
     * 当前项目版本号
     */
    public static final Version version = new Version(1, 0, 1);

    /**
     * 开发环境
     */
    private Environment environment = DataProviderManager.getDataProvider().getAppEnvironment();

    /**
     * 登录用户
     */
    private User user;

    /**
     * 单例
     *
     * @return
     */
    public static ConfigManager getInstance() {
        return configManager;
    }

    /**
     * 初始化方法
     */
    public void init() {
        int i = this.environment == null ? Environment.ONLINE.ordinal() : this.environment.ordinal();
        INIT_SERVER_HOST = new String[]{"http://16794ui705.iok.la:32542", "https://store.ecbao.cn", "https://store.ecbao.cn"}[i];
        INIT_PAY_HOST = new String[]{"http://16794ui705.iok.la:29890", "https://trade.ecbao.cn", "https://trade.ecbao.cn"}[i];

        if (environment.ordinal() == Environment.ONLINE.ordinal()) {
            DEBUG = false;
        }
        initAppDir();
    }

    /**
     * 初始化应用存储目录
     */
    public void initAppDir() {
        File _FileDir = new File(DIR_IMAGE);
        if (!_FileDir.exists()) _FileDir.mkdirs();
        _FileDir = new File(DIR_CACHE);
        if (!_FileDir.exists()) _FileDir.mkdirs();
        _FileDir = new File(DIR_ERROR);
        if (!_FileDir.exists()) _FileDir.mkdirs();
        _FileDir = new File(DIR_LOG);
        if (!_FileDir.exists()) _FileDir.mkdirs();
    }

    //--------- get set------------
    public Environment getEnvironment() {
        return this.environment;
    }

    public void setEnvironment(Environment paramEnvironment) {
        this.environment = paramEnvironment;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User userInfo) {
        this.user = userInfo;
        user.setShopName(user.getShop().getShopName());
        user.setShopImageUrl(user.getShop().getImageUrl());
    }

    public String getRegId() {
        regId = PreferencesUtils.getString(DataProviderManager.getContext(), "regId", "");
        return regId;
    }

    public void setRegId(String regId) {
        this.regId = regId;
        PreferencesUtils.putString(DataProviderManager.getContext(), "regId", regId);
    }
}
